package com.ht.system.controller.login;

import cn.hutool.core.bean.BeanUtil;
import com.ht.common.core.constant.HttpStatus;
import com.ht.common.core.domain.LoginUser;
import com.ht.common.core.domain.R;
import com.ht.common.core.utils.JwtUtils;
import com.ht.common.core.utils.StringUtils;
import com.ht.common.core.web.domain.AjaxResult;
import com.ht.common.security.annotation.InnerAuth;
import com.ht.common.security.auth.AuthUtil;
import com.ht.common.security.service.TokenService;
import com.ht.common.security.utils.SecurityUtils;
import com.ht.system.api.dto.LoginBodyDto;
import com.ht.system.domain.SysMenu;
import com.ht.system.domain.SysUser;
import com.ht.system.domain.vo.LoginBody;
import com.ht.system.service.impl.SysLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * token 控制
 *
 * @author ruoyi
 */
@RestController
public class TokenController
{
    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;

    @PostMapping("login")
    public AjaxResult login(@RequestBody LoginBody form)
    {
        AjaxResult ajAjaxResult=new AjaxResult();

        // 用户登录
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        Map map= tokenService.createToken(userInfo);
        ajAjaxResult.put("code",200);
        ajAjaxResult.put("msg","登录成功");
        ajAjaxResult.put("token",map.get("access_token"));
        ajAjaxResult.put("expires_in",map.get("expires_in"));
        // 获取登录token
        return  ajAjaxResult;
    }

    @PostMapping("logout")
    public AjaxResult logout(HttpServletRequest request)
    {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username);
        }
        return AjaxResult.success("成功");
    }
    
    @InnerAuth
    @PostMapping("wxLogin")
    public R<AjaxResult> wxLogin(@RequestBody LoginBodyDto loginBodyDto)
    {
        LoginBody form = BeanUtil.toBean(loginBodyDto, LoginBody.class);
        AjaxResult ajAjaxResult=new AjaxResult();
        // 用户登录
        AjaxResult result = sysLoginService.wxLogin(form.getUsername(), form.getPassword());
        if(HttpStatus.ERROR == (Integer)result.get("code")){
          return R.fail(result);
        }
        LoginUser userInfo = (LoginUser)result.get("data");
//         LoginUser userInfo = sysLoginService.wxLogin(form.getUsername(), form.getPassword());
        Map map= tokenService.createToken(userInfo);
        ajAjaxResult.put("code",200);
        ajAjaxResult.put("msg","登录成功");
        ajAjaxResult.put("token",map.get("access_token"));
        ajAjaxResult.put("expires_in",map.get("expires_in"));
        // 获取登录token
        return  R.ok(ajAjaxResult);
    }

}
